package com.ldc.api.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ldc.common.utils.DateTimeUtils;
import com.ldc.model.UserInfo;
import com.ldc.model.base.ApiBean;
import com.ldc.service.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能描述
 *
 * @author liudi
 * @since 2023/7/22
 */
@Slf4j
@RestController
@RequestMapping(value = "/userinfo")
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;

    /**
     * 编辑用户信息
     *
     * @param userInfo
     * @return
     */
    @PostMapping("/edit")
    public ApiBean<?> edit(@RequestBody UserInfo userInfo) {
        long time = DateTimeUtils.getTime();
        if (userInfo == null) {
            return ApiBean.failed(false);
        }
        UserInfo currentUser = userInfoService.findByUserId(userInfo.getUid());
        if (currentUser == null) {
            return ApiBean.success(false);
        }
        currentUser.setGander(userInfo.getGander());
        currentUser.setEmail(userInfo.getEmail());
        currentUser.setMobile(userInfo.getMobile());
        currentUser.setGander(userInfo.getGander());
        currentUser.setNikeName(userInfo.getNikeName());
        currentUser.setAddress(userInfo.getAddress());
        currentUser.setUserIcon(userInfo.getUserIcon());
        currentUser.setChangeTime(time);
        boolean success = userInfoService.update(currentUser, new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUid, userInfo.getUid()));
        return ApiBean.success(success);
    }
}
